## Perspective Taking and Security Dilemma Thinking: replication data
#Kertzer, Brutger and Quek
#October 1, 2023

####### SCS Replication 1.R: this R file contains the code necessary to load the datasets and generate cleaned variables for the analysis - run this file first!
# All of the following analyses were carried out using R version 4.3.1 GUI 1.77 (Big Sur ARM build 8238) on an M1 Pro Macbook Pro running MacOS Ventura. 

library(here)
library(stargazer)
library(ggplot2)
library(haven)
library(interflex)
library(tm)
library(stm)
library(AER)
library(readxl)
library(tidyverse)
library(magrittr)
library(lme4)
library(performance)

china1 <- get(load("SCS China Data.RData"))
usa1 <- get(load("SCS US Data.RData"))
usa2 <- get(load("SCS Hawaii Data.RData"))

recode <- function(variable, reverse=FALSE, maxVal, binarize=FALSE, x=NA, x2){
	if (is.factor(variable)){variable <- as.numeric(variable)}
	if (is.character(variable)){variable <- as.numeric(variable)}
	if (missing(maxVal)){maxVal <- max(variable, na.rm=TRUE)}
	variable[variable > maxVal] <- NA
	if (reverse){
		temp <- variable
		for (j in 1:maxVal){
			temp[which(variable==j)] <- maxVal-j+1
		}
		return(temp)
	}
	if (binarize){
		if (is.na(x)){stop("Specify the value to dichotomize on")}
		temp <- variable
		if (missing(x2)){
			i <- which(variable == x)
			k <- which(variable < x | variable > x)
		}
		else{
			i <- which(variable >= x & variable <= x2)
			k <- which(variable < x | variable > x2)
		}
		temp[i] <- 1
		temp[k] <- 0
		return(temp)
	}
	return(variable)
}

rescale <- function(x){
	return((x-min(x,na.rm=TRUE))/(max(x-min(x,na.rm=TRUE),na.rm=TRUE)))
}

#Because of missing values, define a new sum function
sum2 <- function(x){
	z <- x[!is.na(x)]; ifelse(length(z), sum(z), NA)
}

### Coding for US study 1

usa1$policyChoice <- ifelse(usa1$choice==1,ifelse(usa1$Incr1==7,7,ifelse(usa1$Incr1==6,6,NA)),ifelse(usa1$choice==2, ifelse(usa1$Incr2==1,1,ifelse(usa1$Incr2==2,2,NA)),  ifelse(usa1$choice==3, ifelse(usa1$Incr3==3,3,ifelse(usa1$Incr3==4,4,ifelse(usa1$Incr3==5,5,NA))),NA))) #Policy choice

usa1$attrBias <- usa1$attrChInc - usa1$attrUsInc #Attribution asymmetry

#Calculate k-level reasoning measures (either 0,1,2, or 3, based on the demarcation point it's closest to)
calculate.k <- function(x){
	out <- cbind(x, k0dist=rep(NA, length(x)), k1dist=rep(NA, length(x)), k2dist=rep(NA, length(x)), k3dist=rep(NA, length(x)), k=rep(NA, length(x)))
	out[,2] <- round((out[,1]-(50*0.6^0))^2, digits=0)
	out[,3] <- round((out[,1]-(50*0.6^1))^2, digits=0)
	out[,4] <- round((out[,1]-(50*0.6^2))^2, digits=0)
	out[,5] <- round((out[,1]-(50*0.6^3))^2, digits=0)
	for (i in 1:nrow(out)){
		out[i,6] <- ifelse(is.na(out[i,1]),NA,order(out[i,2:5])[1]-1)
	}
	return(out[,6])
}

usa1$k_level <- calculate.k(usa1$beauty)
usa1$k_levelChn <- calculate.k(usa1$beautyChn)

#National attachment and chauvinism
na1 <- as.numeric(usa1$natAttach1)
na2 <- as.numeric(usa1$natAttach2)
usa1$natAboutYou <- rescale(na1)
usa1$natAttach1 <- rescale(apply(cbind(na1,na2),1,sum2))
nc1 <- as.numeric(usa1$natChauv1)
usa1$natChauv1 <- rescale(nc1)

#Militant assertiveness
ma1 <- as.numeric(usa1$milAssert1)
ma2 <- as.numeric(usa1$milAssert2)
ma3 <- as.numeric(usa1$milAssert3)
usa1$milAssert1 <- rescale(apply(cbind(ma1, ma2, ma3),1,sum2))
usa1$imgNegInt1 <- rescale(usa1$imgNegInt)
usa1$imgParity1 <- rescale(usa1$imgParity)

#Interpersonal reactivity index
iri1 <- as.numeric(usa1$PT_1)
iri2 <- as.numeric(usa1$PT_2)
iri3 <- as.numeric(usa1$PT_3)
usa1$iri1 <- rescale(apply(cbind(iri1, iri2, iri3), 1,sum2))

#Ideology
usa1$ideo1 <- rescale(as.numeric(usa1$ideology))

#Party ID
usa1$pid1 <- rescale(ifelse(usa1$party==1,ifelse(usa1$partyDem==1,1,ifelse(usa1$partyDem==2,2,NA)),ifelse(usa1$party==2,ifelse(usa1$partyRep==1,7,ifelse(usa1$partyRep==2,6,NA)), ifelse(usa1$party>=3, ifelse(usa1$partyIndep==1,5,ifelse(usa1$partyIndep==2,3,ifelse(usa1$partyIndep==3,4,NA))),NA))))

#Political knowledge
k1 <- recode(usa1$polKnow1,binarize=TRUE,x=1)
k2 <- recode(usa1$polKnow2,binarize=TRUE,x=1)
k3 <- recode(usa1$polKnow3,binarize=TRUE,x=1)
k4 <- recode(usa1$polKnow4,binarize=TRUE,x=1)
k5 <- recode(usa1$polKnow5,binarize=TRUE,x=3)
usa1$know1 <- rescale(apply(cbind(k1,k2,k3,k4,5), 1,sum2))

usa1$male <- recode(usa1$gender, binarize=TRUE, x=1) #Male
usa1$income1 <- rescale(as.numeric(usa1$income)) #Income

#Education categories
usa1$educ0 <- recode(usa1$edu, binarize=TRUE, x=1, x2=2) #High school or less
usa1$educ1 <- recode(usa1$edu, binarize=TRUE, x=3) #Some College
usa1$educ2 <- recode(usa1$edu, binarize=TRUE, x=4, x2=5) #College/university
usa1$educ3 <- recode(usa1$edu, binarize=TRUE, x=6, x2=8) #Grad/prof school
usa1$educ <- rescale(as.numeric(usa1$edu)) #Rescaled


#Response latencies
usa1$lscreen3_t <- log(usa1$screen3_t)

### Coding for China study

china1$policyChoice <- apply(cbind(china1$Incr1, china1$Incr2, china1$Incr3),1,sum2)

china1$attrBias <- china1$attrUsInc - china1$attrChInc #Attribution asymmetry

china1$k_level <- calculate.k(china1$beauty)
china1$k_levelUS <- calculate.k(china1$beautyUS)

china1$imgNegInt1 <- rescale(china1$imgNegInt)
china1$imgParity1 <- rescale(china1$imgParity)

china1$iri1 <- rescale(apply(cbind(china1$iri1, china1$iri2, china1$iri3), 1, sum2))

china1$milAssert1 <- rescale(apply(cbind(china1$ma1,china1$ma2),1,sum2))

k1 <- recode(china1$know1, binarize=TRUE, x=1)
k2 <- recode(china1$know2, binarize=TRUE, x=4)
k3 <- recode(china1$know3, binarize=TRUE, x=3)
k4 <- recode(china1$know4, binarize=TRUE, x=2)
china1$know1 <- rescale(apply(cbind(k1,k2,k3,k4),1,sum2))

china1$male <- recode(china1$sex, binarize=TRUE, x=1)

#Ethnicity (Han Chinese dummy)
china1$Han <- recode(china1$ethnic, binarize=TRUE, x=1)

#Married?
china1$married <- recode(china1$marital, binarize=TRUE, x=2)

#Education
china1$educ <- rescale(china1$edu1)

#Party member
china1$partyMember <- recode(china1$party, binarize=TRUE, x=1)

#Income
china1$income1[which(china1$income1==0)] <- NA
china1$income1 <- rescale(china1$income1)

#Nationalism
china1$nc4[which(china1$nc4==0)] <- NA #Something bad about you

china1$natAboutYou <- rescale(china1$nc4)

china1$lscreen3_t <- log(china1$screen3_t)

#### Weight generation for USA study 2 (Hawaii)

### Code block to generate weights using Stata

# usa2$white <- recode(usa2$race, binarize=TRUE, x=1)
# edu <- rep(NA, nrow(usa2))
# edu[which(usa2$edu <=2)] <- 1 #High school or less
# edu[which(usa2$edu ==3)] <- 2 #Some college
# edu[which(usa2$edu ==4 | usa2$edu ==5)] <- 3 #College/university
# edu[which(usa2$edu >=6)] <- 4 #Postgraduate degree
# usa2$age2 <- as.numeric(usa2$age >=25 & usa2$age <=44)
# usa2$age3 <- as.numeric(usa2$age >=45 & usa2$age <=64)
# usa2$age4 <- as.numeric(usa2$age >=65)
# usa2$educ1 <- rescale(edu)
# usa2$educ2 <- recode(edu, binarize=TRUE, x=1)
# usa2$educ3 <- recode(edu, binarize=TRUE, x=2)
# usa2$educ4 <- recode(edu, binarize=TRUE, x=3)
# usa2$Rep <- as.numeric(usa2$pid >= 4)
# usa2$Ind <- as.numeric(usa2$pid == 3)
# usa2$male <- recode(usa2$gender, binarize=TRUE, x=1)

#Generate weights for Hawaii sample
#c(prop.table(table(usa1$White))[2], prop.table(table(usa1$educ1))[2], prop.table(table(usa1$educ2))[2], prop.table(table(usa1$educ3))[2], prop.table(table(usa1$pid1 >=0.8))[2], prop.table(table(usa1$pid1 >=0.3 & usa1$pid1 <= 0.8))[2], prop.table(table(usa1$age>= 25 & usa1$age<=44))[2], prop.table(table(usa1$age>= 45 & usa1$age<=64))[2], prop.table(table(usa1$age>=65))[2], prop.table(table(usa1$male))[2])

#write_dta(usa2, path="SCS Hawaii Data.dta", version=13)

#Open the .dta file in Stata and run the following code:
#ebalance white educ2 educ3 educ4 Rep Ind age2 age3 age4 male, manualtargets(0.7805044 0.1192098 0.3664850 0.1723433 0.2724230 0.3293173 0.3268587 0.3375754 0.2042867 0.4507703)
#recode _webal (5/25=5), gen(eWeight)
#keep attrChInc attrUsInc race age gender pid eWeight
#save "SCS Hawaii Data.dta", replace
#usa2 <- read_dta("SCS Hawaii Data.dta")
#save(usa2, file="SCS Hawaii Data.RData")

